package com.alstudio.ui.module.props;

import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.pojo.module.local.Prop;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.utils.formater.FileIdUtils;
import com.loovee.imaohu.R;

public class PropItem extends FrameLayout {

	public static final int TYPE_MALL_PROPS = 1;
	public static final int TYPE_MY_PROPS = 2;
	private int type = TYPE_MALL_PROPS;

	private String recycleTag = "";

	private Prop prop1 = null;
	private Prop prop2 = null;
	private Prop prop3 = null;
	private Prop prop4 = null;

	private OnItemClickListener listener = null;

	private FrameLayout back;
	private TextView text_title;
	private ImageView image_prop1, image_prop2, image_prop3, image_prop4;
	private ImageView image_hot1, image_hot2, image_hot3, image_hot4;
	private TextView text_prop_name1, text_prop_name2, text_prop_name3,
			text_prop_name4;

	public PropItem(BaseActivity context, int type) {
		super(context);
		this.type = type;

		ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
				.inflate(R.layout.prop_item, this);

		initView();
	}

	public void setOnItemClickListener(OnItemClickListener l) {
		this.listener = l;
	}

	public void setRecycleTag(String recycleTag) {
		this.recycleTag = recycleTag;
	}

	private void initView() {
		back = (FrameLayout) findViewById(R.id.back);
		text_title = (TextView) findViewById(R.id.text_title);
		image_prop1 = (ImageView) findViewById(R.id.image_prop1);
		image_prop2 = (ImageView) findViewById(R.id.image_prop2);
		image_prop3 = (ImageView) findViewById(R.id.image_prop3);
		image_prop4 = (ImageView) findViewById(R.id.image_prop4);
		image_hot1 = (ImageView) findViewById(R.id.image_hot1);
		image_hot2 = (ImageView) findViewById(R.id.image_hot2);
		image_hot3 = (ImageView) findViewById(R.id.image_hot3);
		image_hot4 = (ImageView) findViewById(R.id.image_hot4);
		text_prop_name1 = (TextView) findViewById(R.id.text_prop_name1);
		text_prop_name2 = (TextView) findViewById(R.id.text_prop_name2);
		text_prop_name3 = (TextView) findViewById(R.id.text_prop_name3);
		text_prop_name4 = (TextView) findViewById(R.id.text_prop_name4);

		image_prop1.setOnClickListener(onClickListener);
		image_prop2.setOnClickListener(onClickListener);
		image_prop3.setOnClickListener(onClickListener);
		image_prop4.setOnClickListener(onClickListener);
	}

	public void setProp(Prop p1, Prop p2, Prop p3, Prop p4) {
		this.prop1 = p1;
		this.prop2 = p2;
		this.prop3 = p3;
		this.prop4 = p4;

		showProp(prop1, image_prop1, image_hot1, text_prop_name1);
		showProp(prop2, image_prop2, image_hot2, text_prop_name2);
		showProp(prop3, image_prop3, image_hot3, text_prop_name3);
		showProp(prop4, image_prop4, image_hot4, text_prop_name4);
	}

	public void setBackground(int resid) {
		back.setBackgroundResource(resid);
	}

	public void setTitle(String title) {
		if (TextUtils.isEmpty(title)) {
			text_title.setText("");
		} else {
			text_title.setText(title);
		}
	}

	public void setItemClickListener(OnItemClickListener l) {
		this.listener = l;
	}

	private void showProp(Prop p, ImageView viewIcon, ImageView viewHot,
			TextView viewName) {
		if (p == null) {
			viewIcon.setVisibility(View.INVISIBLE);
			viewName.setVisibility(View.INVISIBLE);
			viewHot.setVisibility(View.INVISIBLE);

		} else {
			showImage(viewIcon, p);
			viewName.setText(p.getPropName());

			viewIcon.setVisibility(View.VISIBLE);
			viewName.setVisibility(View.VISIBLE);
			viewHot.setVisibility(View.INVISIBLE);
		}

		viewIcon.setTag(p);
	}

	private void showImage(ImageView v, Prop p) {
		v.setImageBitmap(null);

		String fileid = "";
		fileid = FileIdUtils.getBareFileId(p.getPropFileId());

		if (!TextUtils.isEmpty(fileid)) {
			ALLocalEnv.getLocalEnv().displayImagePluginServerlet(fileid, v);
		}
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (listener != null) {
				Prop p = (Prop) v.getTag();
				if (p != null) {
					listener.onItemClick(p);
				}
			}

		}
	};

	public interface OnItemClickListener {
		public void onItemClick(Prop p);
	}
}
